package com.briup.web;/*
 * @Description:
 * @Author: 枸男人
 * @Date: 2024/12/03/09:12
 * @LastEditors: 34866
 * @: 你说哥帅,哥笑了,因为哥笑起来更帅
 */

import com.briup.pojo.Student;
import com.briup.pojo.StudentWithAddress;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/path")
//类级别的RequestMapping 定义了这个程序类中所有方法的公共请求路径前缀
@RestController
//类级别的RestController 定义了这个程序类中所有方法返回的数据都是json格式
public class PathController {
    @RequestMapping("/test")//处理具体的请求的路径
    public String tets() {
        System.out.println("in test()");
        return "Hello,PathController,test()";
    }
    //多个路径
//    @RequestMapping(path = {"/hello","/hi"})//通过多个路径
    @RequestMapping(value = {"/hello","/hi"})//通过多个路径请求方法
    public String hello(){
        System.out.println("in hello()");
        return "hello,PathController,hello()";
    }

    //指定请求方法
    @RequestMapping(value = "/method",method ={ RequestMethod.POST,RequestMethod.GET})//通过value属性指定请求路径 method属性指定请求方法
    public String method(){
        System.out.println("in method()");
        return "hello,PathController,method()";
    }
    //获取请求参数
    @RequestMapping(value = "/getParams",method ={ RequestMethod.POST,RequestMethod.GET})
    public String getParams(HttpServletRequest request){
        //获取请求参数
        String name = request.getParameter("name");
        String ageStr = request.getParameter("age");
        int age = Integer.parseInt(ageStr);
        System.out.println("name:"+name);
        System.out.println("age:"+age);
        System.out.println("in getParams()");
        return "ok!拿捏";
    }
    //参数获取
    @RequestMapping(value = "/simpleParams",method ={ RequestMethod.POST,RequestMethod.GET})
    public String simpleParams(String name ,Integer age){
        System.out.println("name:"+name);
        System.out.println("age:"+age);
        return "ok!拿捏";
    }
    @RequestMapping(value = "/simpleParams2",method ={ RequestMethod.POST,RequestMethod.GET})
    public String simpleParams2(@RequestParam("username") String name , Integer age){
        System.out.println("name:"+name);
        System.out.println("age:"+age);
        return "ok!拿捏";
    }
    //实体类获取参数
    @RequestMapping(value = "/simplePojo",method ={ RequestMethod.POST,RequestMethod.GET})
    public String simplePojo(Student stu){
        System.out.println(stu);
        return "ok!拿捏";
    }
    //嵌套——实体类获取参数
    @RequestMapping(value = "/complexPojo",method ={ RequestMethod.POST,RequestMethod.GET})
    public String complexPojo(StudentWithAddress stu){
        System.out.println(stu);
        return "ok!拿捏";
    }
    //数组参数——实体类获取参数
    @RequestMapping(value = "/arrayParam",method ={ RequestMethod.POST,RequestMethod.GET})
    public String arrayParam(String[] likes){
        System.out.println(Arrays.toString(likes));
        return "ok!拿捏";
    }
    //集合参数——实体类获取参数
    @RequestMapping(value = "/listParam",method ={ RequestMethod.POST,RequestMethod.GET})
    public String listParam(@RequestParam List<String> likes){
        System.out.println(likes);
        return "ok!拿捏";
    }
    //json参数——实体类获取参数 必须通过Post请求方式才能发送
    @RequestMapping(value = "/jsonParam",method ={ RequestMethod.POST})
    public String jsonParam(@RequestBody StudentWithAddress stu){
        System.out.println(stu);
        return "ok!拿捏🤌";
    }
    //根据id删除用户
    @RequestMapping(value = "/deleteById/{id}")
    public String jsonParam(@PathVariable Integer id){
        System.out.println(id);
        return "ok!拿捏🤌";
    }
    //根据查询用户
    @RequestMapping(value = "/query/{id}/{username}")
    public String query(@PathVariable Integer id,
                        @PathVariable("username") String username){
        System.out.println(id);
        System.out.println(username);
        return "ok!拿捏🤌";
    }
    @RequestMapping(value = "/queryStuById/{id}")
    @ResponseBody//如果返回对象的josn格式数据需要借助@ResponseBody实现
    public Student queryStuById(@PathVariable Integer id){
        System.out.println(id);
        Student student = new Student();
        student.setName("张三");
        student.setAge(20);
        return student;
    }
    //根据条件查询用户
    @RequestMapping("/quryStu/{id}" )
    @ResponseBody
    public List<Student> quryStu(@PathVariable Integer id){
        System.out.println(id);
        //返回对象 返回json
        Student student1 = new Student();
        student1.setName("张三");
        student1.setAge(18);
        Student student2 = new Student();
        student2.setName("王五");
        student2.setAge(18);
        ArrayList<Student> students = new ArrayList<>();
        students.add(student1);
        students.add(student2);
        return students;
    }
}
